package com.yami.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.common.dto.DistributionUserWalletBillDto;
import com.yami.shop.distribution.common.dto.DistributionUserWalletDto;
import com.yami.shop.distribution.common.model.DistributionUser;
import com.yami.shop.distribution.common.service.DistributionUserService;
import com.yami.shop.distribution.common.service.DistributionUserWalletBillService;
import com.yami.shop.distribution.common.service.DistributionUserWalletService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author yami
 */
@RestController
@RequestMapping("/p/distribution/wallet")
@Tag(name = "分销钱包接口")
public class DistributionUserWalletController {
    @Autowired
    private DistributionUserWalletService distributionUserWalletService;
    @Autowired
    private DistributionUserService distributionUserService;
    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;

    @GetMapping("/info")
    @Operation(summary =  "查看分销员钱包数据")
    public ServerResponseEntity<DistributionUserWalletDto> getDistributionUserWalletDtoByDistributionUserId() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是销售员
            throw new YamiShopBindException("yami.distribution.user.no.distributor");
        }
        return ServerResponseEntity.success(distributionUserWalletService.getDistributionUserWalletDtoByDistributionUserId(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/page")
    @Operation(summary =  "查看分销员钱包流水记录")
    @Parameters({
            @Parameter(name = "orderBy", description = "时间排序(0升序 1降序) 默认1")
    })
    public ServerResponseEntity<IPage<DistributionUserWalletBillDto>> getDistributionUserWalletBillDtoPage(PageParam<DistributionUserWalletBillDto> page,
                                                                                                     @RequestParam(name="orderBy", defaultValue = "1") Integer orderBy) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ServerResponseEntity.success(distributionUserWalletBillService
                .getDistributionUserWalletBillDtoPage(page, distributionUser.getDistributionUserId(), orderBy));
    }
}
